package com.shu.logsearch.core.controller;

import com.shu.logsearch.common.Constant.ViewConstant;
import com.shu.logsearch.es.model.LogDoc;
import com.shu.logsearch.es.service.ESRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * 首页
 */
@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    private ESRestService esRestService;

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/search")
    public String search(Model model, @RequestParam("keyword") String keyWord) {
        if (StringUtils.isEmpty(keyWord))
            return ViewConstant.INDEX_VIEW;
        String[] indices = {LogDoc.INDEX_NAME};
        String[] fieldNames = {"title", "content"};
        List<Map<String, Object>> fileList = esRestService.searchDocs(indices,keyWord,fieldNames,1,10);
        model.addAttribute("fileList", fileList);
        model.addAttribute("keyword", keyWord);
        return ViewConstant.RESULT_VIEW;
    }
}
